package com.xinongbest.mall.controller;

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(2);



//        ExecutorService executor = Executors.newFixedThreadPool(1);
//        Callable<Integer> callable = () -> {
//            System.out.println("当前线程ID：" + Thread.currentThread().getId());
//            Thread.sleep(3000);
//            return 100;
//        };
//        Future<Integer> future = executor.submit(callable);
//        try {
//            Integer result = future.get(5, TimeUnit.SECONDS);
//            System.out.println("当前线程ID：" + Thread.currentThread().getId());
//            System.out.println(result);
//        } catch (InterruptedException | ExecutionException | TimeoutException e) {
//            e.printStackTrace();
//        }
//        executor.shutdown();
    }
}

class MyThread extends Thread {
    public MyThread() {
        super("MyThread");
    }

    @Override
    public void run() {
        System.out.println("当前线程ID：" + Thread.currentThread().getId());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前线程ID：" + Thread.currentThread().getId());
    }

    public static void main(String[] args) throws InterruptedException {
        Thread work = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("I'm doing my work -- " + i);
                try {
                    System.out.println("I will sleep");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("My sleeping was interrupted");
                }
                System.out.println("I'm interrupted ? " + Thread.currentThread().isInterrupted());
            }
        });
        work.start();
        Thread.sleep(1);
        work.interrupt();
    }
}
